<html>
<head><meta charset="utf-8"><title>question · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/question.html">question</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="185857764"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/question/near/185857764" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> std::Veetaha <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/question.html#185857764">(Jan 16 2020 at 20:43)</a>:</h4>
<p>Can someone please explain what is the difference between <code>flatten_mut()</code> and <code>flatten_const()</code> methods in this code? I see that both of them have the same income and outcome.</p>



<a name="185857781"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/question/near/185857781" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> std::Veetaha <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/question.html#185857781">(Jan 16 2020 at 20:43)</a>:</h4>
<p><a href="https://github.com/CAD97/thin-dst/blob/1da8b7ac72829b1ed57075d13810bc2ce9842e0b/src/lib.rs#L111" target="_blank" title="https://github.com/CAD97/thin-dst/blob/1da8b7ac72829b1ed57075d13810bc2ce9842e0b/src/lib.rs#L111">https://github.com/CAD97/thin-dst/blob/1da8b7ac72829b1ed57075d13810bc2ce9842e0b/src/lib.rs#L111</a></p>



<a name="185861002"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/question/near/185861002" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pksunkara <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/question.html#185861002">(Jan 16 2020 at 21:13)</a>:</h4>
<p>but <code>fatten_mut</code> uses <code>make_slice_mut</code> which probably cuts things from the given pointer</p>



<a name="185861240"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/question/near/185861240" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> std::Veetaha <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/question.html#185861240">(Jan 16 2020 at 21:15)</a>:</h4>
<p>Those are just wrappers for <code>slice::from_raw_parts()</code> and <code>slice::from_raw_parts_mut()</code><br>
<a href="https://github.com/CAD97/thin-dst/blob/1da8b7ac72829b1ed57075d13810bc2ce9842e0b/src/polyfill.rs#L9" target="_blank" title="https://github.com/CAD97/thin-dst/blob/1da8b7ac72829b1ed57075d13810bc2ce9842e0b/src/polyfill.rs#L9">https://github.com/CAD97/thin-dst/blob/1da8b7ac72829b1ed57075d13810bc2ce9842e0b/src/polyfill.rs#L9</a></p>



<a name="185899197"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/question/near/185899197" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/question.html#185899197">(Jan 17 2020 at 09:19)</a>:</h4>
<p>They give different pointer provenance. One goes via <code>&amp;</code>, the other via <code>&amp;mut</code>. If you intend to mutate the thing, you need to get the raw pointer from an <code>&amp;mut</code>.</p>



<a name="185934966"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/question/near/185934966" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/question.html#185934966">(Jan 17 2020 at 16:55)</a>:</h4>
<p>provenance?</p>



<a name="185935228"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/question/near/185935228" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/question.html#185935228">(Jan 17 2020 at 16:57)</a>:</h4>
<p>yup: <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/52" target="_blank" title="https://github.com/rust-lang/unsafe-code-guidelines/issues/52">https://github.com/rust-lang/unsafe-code-guidelines/issues/52</a>, <a href="https://github.com/rust-lang/rust/pull/54461" target="_blank" title="https://github.com/rust-lang/rust/pull/54461">https://github.com/rust-lang/rust/pull/54461</a></p>
<p>Basically, it's only valid to write through <code>*mut</code> if it originated (has a provenance of?) from a mutable location</p>



<a name="185935474"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/question/near/185935474" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/question.html#185935474">(Jan 17 2020 at 16:59)</a>:</h4>
<p>oh, so like <code>*(&amp;t as *const _ as *mut) = whatever</code> is invalid</p>



<a name="185935683"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/question/near/185935683" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/question.html#185935683">(Jan 17 2020 at 17:01)</a>:</h4>
<p>right</p>



<a name="185935729"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/question/near/185935729" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/question.html#185935729">(Jan 17 2020 at 17:01)</a>:</h4>
<p>well, I don't know a lot about this stuff myself, but that's my semi-layman understanding</p>



<a name="185935942"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/question/near/185935942" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/question.html#185935942">(Jan 17 2020 at 17:03)</a>:</h4>
<p><em>let me go change some code at $WORK</em></p>



<a name="185968306"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/question/near/185968306" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> std::Veetaha <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/question.html#185968306">(Jan 17 2020 at 23:38)</a>:</h4>
<p><span class="user-mention" data-user-id="133169">@matklad</span> , wow that is something I never knew, this provenance thing is something complex. I've read <a href="https://www.nccgroup.trust/us/about-us/newsroom-and-events/blog/2019/june/pointer-provenance/" target="_blank" title="https://www.nccgroup.trust/us/about-us/newsroom-and-events/blog/2019/june/pointer-provenance/">this post</a> and if I understood it correctly provenance is not something that is standardized but instead used exclusively by the compiler. There is also a post that justifies that <a href="https://www.ralfj.de/blog/2018/07/24/pointers-and-bytes.html" target="_blank" title="https://www.ralfj.de/blog/2018/07/24/pointers-and-bytes.html">pointers are not just integers</a>. I'd make a conclusion that provenance is the static metadata used for optimizations, that is erased in the compiled output, and at runtime, pointers are indeed just integers.<br>
But I am not sure how this is applicable to the context I gave initially. This seems to be used only for detecting pointers aliasing I am not sure how this applies to *mut and *const context.</p>



<a name="186590601"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/question/near/186590601" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christopher Durham <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/question.html#186590601">(Jan 25 2020 at 22:06)</a>:</h4>
<p>I wrote a good bit over on <a href="https://github.com/CAD97/thin-dst/issues/9" target="_blank" title="https://github.com/CAD97/thin-dst/issues/9">the issue you posted</a>. It is about pointer provenance, but the real issue is that <code>make_mut</code>/<code>make_slice_mut</code> necessarily create references, and going through the references puts</p>



<a name="186616839"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/question/near/186616839" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> std::Veetaha <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/question.html#186616839">(Jan 26 2020 at 11:07)</a>:</h4>
<p>Sorry for the <code>junk</code> dir, it is actually the place where I test things out )</p>



<a name="186617175"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/question/near/186617175" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> std::Veetaha <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/question.html#186617175">(Jan 26 2020 at 11:16)</a>:</h4>
<p><span class="user-mention" data-user-id="132829">@Christopher Durham</span>, thank you for the detailed answer! While trying to understand the pointer cast from <code>*const [SliceItem]</code> to <code>NonNull&lt;ThinData&lt;Head, SliceItem&gt;&gt;</code> I cloned your repo and ran <code>cargo build</code>, but it failed:</p>
<div class="codehilite"><pre><span></span>~/my/junk/thin-dst (master) $ rustc --version
rustc 1.40.0 (73528e339 2019-12-16)
~/my/junk/thin-dst (master) $ cargo build
   Compiling thin-dst v1.0.0 (/home/veetaha/my/junk/thin-dst)
error[E0210]: type parameter `Head` must be used as the type parameter for some local type (e.g., `MyStruct&lt;Head&gt;`)
   --&gt; src/lib.rs:433:6
    |
433 | impl&lt;Head, SliceItem&gt; From&lt;ThinArc&lt;Head, SliceItem&gt;&gt; for Arc&lt;ThinData&lt;Head, SliceItem&gt;&gt; {
    |      ^^^^ type parameter `Head` must be used as the type parameter for some local type
    |
    = note: only traits defined in the current crate can be implemented for a type parameter

error[E0210]: type parameter `Head` must be used as the type parameter for some local type (e.g., `MyStruct&lt;Head&gt;`)
   --&gt; src/lib.rs:492:6
    |
492 | impl&lt;Head, SliceItem&gt; From&lt;ThinRc&lt;Head, SliceItem&gt;&gt; for Rc&lt;ThinData&lt;Head, SliceItem&gt;&gt; {
    |      ^^^^ type parameter `Head` must be used as the type parameter for some local type
    |
    = note: only traits defined in the current crate can be implemented for a type parameter

error[E0210]: type parameter `Head` must be used as the type parameter for some local type (e.g., `MyStruct&lt;Head&gt;`)
   --&gt; src/lib.rs:574:6
    |
574 | impl&lt;Head, SliceItem&gt; From&lt;ThinPtr&lt;Head, SliceItem&gt;&gt; for NonNull&lt;ThinData&lt;Head, SliceItem&gt;&gt; {
    |      ^^^^ type parameter `Head` must be used as the type parameter for some local type
    |
    = note: only traits defined in the current crate can be implemented for a type parameter

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0210`.
error: could not compile `thin-dst`.
</pre></div>



<a name="186623275"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/question/near/186623275" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christopher Durham <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/question.html#186623275">(Jan 26 2020 at 14:06)</a>:</h4>
<p>Yeah, head has those two impls that only work on beta currently</p>



<a name="186623328"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/question/near/186623328" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Christopher Durham <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/question.html#186623328">(Jan 26 2020 at 14:08)</a>:</h4>
<p>1.41 adjusts coherence rules a bit so those impls are considered local.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>